package com.ams.admin.controller;

import com.ams.admin.dto.UserAuthDTO;

import com.ams.admin.pojo.Code;
import com.ams.admin.service.ISysUserService;
import com.ams.admin.service.UserService;
import com.ams.admin.utils.wx.*;
import com.ams.common.base.result.R;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author：
 * @date：
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@RestController
@RequestMapping("/api/v1/users")
@Slf4j
@RequiredArgsConstructor
public class UserController {

    private final ISysUserService iSysUserService;

    @Autowired
    private WxAppletUserInfo wxAppletUserInfo;

    @Autowired
    private UserService userService;



    @GetMapping("/username/{username}")
    public R<UserAuthDTO> getUserByUsername(@PathVariable String username) {
        UserAuthDTO user = iSysUserService.getByUsername(username);
        return R.ok(user);
    }

    /**
     * 校验code
     * @param code
     * @return
     */
    @ApiOperation("校验code接口")
    @ResponseBody
    @PostMapping("/login")
    public R login(@RequestBody Code code) {
        String c = code.getCode();
        return userService.getSessionKeyOropenid(c);
    }

    @PostMapping("/authLogin")
    public R authLogin(@RequestBody WXAuth wxAuth) {
        return userService.authLogin(wxAuth);
    }

    @ApiOperation("登录返回用户信息")
    @PostMapping("/toLogin")
    public R toLogin(@RequestParam String sessionId) {
        return userService.toLogin(sessionId);
    }

    /**
     * 刷新token
     * @param token
     * @param refresh
     * @return
     */
    @ApiOperation("刷新token接口")
    @GetMapping("/userinfo")
    public R userinfo(@RequestHeader("Authorization") String token,
                           Boolean refresh){
        return userService.userinfo(token,refresh);
    }

    /**
     * 修改用户信息
     * @param token
     * @param userDto
     * @param sessionId
     * @return
     */
    @ApiOperation("修改用户信息")
    @PutMapping("/updateUser")
    public R updateUser(@RequestHeader("Authorization") String token,
                        @RequestBody UserDto userDto,
                        @RequestParam String sessionId){
        return userService.updateUser(token,userDto,sessionId);
    }

    /**
     * 根据id查询用户信息
     * @param token
     * @param userDto
     * @return
     */
    @ApiOperation("根据id查询用户信息")
    @PostMapping("/findUser")
    public R findUser(@RequestHeader("Authorization") String token,
                      @RequestBody UserDto userDto){
        return userService.findUser(token,userDto);
    }
}